package com.asktgapp.user.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.model.UserInfoVO;
import com.asktgapp.modulebase.common.util.PreferencesUtil;

import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * time:2018/3/19
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

public class SystemPushFragment extends BaseFragment {
    private TextView mContentTv;
    private String mContent;
    private String mID;
    private int type;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_sys_push, container, false);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().setBackNavigationIcon();
        Bundle bundle = getActivity().getIntent().getExtras();
        mID = bundle.getString("id");
        mContentTv = rootView.findViewById(R.id.tv_content);
//        mContentTv.setText(mContent);
        mContent = bundle.getString("content");
        type = bundle.getInt("type", 0);
    }

    @Override
    public void loadData() {
        if (type == 1) {
            visibleLoading();
            Apiservice apiservice = ApiUtil.getInstance().create();
            HashMap params = new HashMap();
            params.put("userid", mID);

            Call<ApiResponseBody<UserInfoVO>> call = apiservice.getUserInfo(params);
            call.enqueue(new Callback<ApiResponseBody<UserInfoVO>>() {
                @Override
                public void onResponse(Call<ApiResponseBody<UserInfoVO>> call, Response<ApiResponseBody<UserInfoVO>> response) {
                    inVisibleLoading();
                    if (response.isSuccessful()) {
                        UserInfoVO bean = response.body().getResult();
                        if (bean.getAuthStatus().equals("1")) {
                            mContentTv.setText("认证成功！");
                        } else if (bean.getAuthStatus().equals("2")) {
                            mContentTv.setText("认证未通过：" + bean.getReason());
                        }
                    } else {
                        showTost(response.raw().message(), 0);
                    }
                }

                @Override
                public void onFailure(Call<ApiResponseBody<UserInfoVO>> call, Throwable t) {
                    inVisibleLoading();
                    visibleNoData();
                    ApiException apiException = ExceptionHandle.handleException(t);
                    showTost(apiException.getMessage(), 0);
                }
            });
        } else if (type == 2) {
            mContentTv.setText(mContent);
        }
    }
}
